
package com.reactlibrary;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Message;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.os.Handler;
import com.baidu.aip.asrwakeup3.core.recog.MyRecognizer;
import com.baidu.aip.asrwakeup3.core.recog.listener.IRecogListener;
import com.baidu.aip.asrwakeup3.core.recog.listener.MessageStatusRecogListener;
import com.baidu.aip.asrwakeup3.core.wakeup.MyWakeup;
import com.baidu.aip.asrwakeup3.core.wakeup.listener.IWakeupListener;
import com.baidu.aip.asrwakeup3.core.wakeup.listener.RecogWakeupListener;
import com.baidu.speech.EventListener;
import com.baidu.speech.asr.SpeechConstant;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
//import com.baidu.aip.asrwakeup3.core.mini.AutoCheck;
import com.baidu.aip.asrwakeup3.core.recog.MyRecognizer;
import com.baidu.aip.asrwakeup3.core.recog.listener.IRecogListener;
import com.baidu.aip.asrwakeup3.core.recog.listener.MessageStatusRecogListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class RNBaiduAsrModule extends ReactContextBaseJavaModule {

  private final ReactApplicationContext reactContext;

  /**
   * 识别控制器，使用MyRecognizer控制识别的流程
   */
  protected MyRecognizer myRecognizer;

  protected MyWakeup myWakeup;

  /*
   * 本Activity中是否需要调用离线命令词功能。根据此参数，判断是否需要调用SDK的ASR_KWS_LOAD_ENGINE事件
   */
  protected boolean enableOffline;

  private static final String TAG = "BaiduASR";



  public RNBaiduAsrModule(ReactApplicationContext reactContext) {
    super(reactContext);
    this.reactContext = reactContext;
  }

  public static void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params)
  {
    System.out.println("reactContext="+reactContext);
    reactContext
        .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
        .emit(eventName, params);
  }

  @ReactMethod
  public void initBaiduAsr(){
    Log.d(TAG, "初始化");
    EventListener listener = new EventListener() {
      @Override
      public void onEvent(String name, String params, byte [] data, int offset, int length) {

        if(name.equals(SpeechConstant.CALLBACK_EVENT_ASR_READY)){
          // 引擎就绪，可以说话，一般在收到此事件后通过UI通知用户可以说话了
          Log.d(TAG, name);
          Log.d(TAG, params);
        }

        if(name.equals(SpeechConstant.CALLBACK_EVENT_ASR_PARTIAL)){
          // 一句话的临时结果，最终结果及语义结果
          Log.d(TAG, name);
          Log.d(TAG, params);
          WritableMap writableMap = Arguments.createMap();
          writableMap.putString("name", name);
          writableMap.putString("data", params);
          writableMap.putInt("offset", offset);
          writableMap.putInt("length", length);
          sendEvent(reactContext, "BaiduAsrResult", writableMap);
        }
        // ... 支持的输出事件和事件支持的事件参数见“输入和输出参数”一节
      }
    };

    myRecognizer = new MyRecognizer(reactContext, listener);
    if (enableOffline) {
      // 基于DEMO集成1.4 加载离线资源步骤(离线时使用)。offlineParams是固定值，复制到您的代码里即可
    }
  }

  @ReactMethod
  public void initWakeUp(){
    Log.d(TAG, "初始化主动唤醒");
    EventListener listener = new EventListener() {
      @Override
      public void onEvent(String name, String params, byte [] data, int offset, int length) {
        Log.d("////", name);
        if(name.equals("wp.data")){
          // 一句话的临时结果，最终结果及语义结果
          Log.d(TAG, name);
          Log.d(TAG, params);
          WritableMap writableMap = Arguments.createMap();
          writableMap.putString("name", name);
          writableMap.putString("data", params);
          writableMap.putInt("offset", offset);
          writableMap.putInt("length", length);
          sendEvent(reactContext, "BaiduAsrWakeup", writableMap);
        }
        if(name.equals("wp.error")){
          Log.d("======", name);
          Log.d("======", params);
        }
        // ... 支持的输出事件和事件支持的事件参数见“输入和输出参数”一节
      }
    };
    myWakeup = new MyWakeup(reactContext,listener);
  }

  @ReactMethod
  public void start(){
    // DEMO集成步骤2.1 拼接识别参数： 此处params可以打印出来，直接写到你的代码里去，最终的json一致即可。
    final Map<String, Object> params = new HashMap<String, Object>();
    Log.i(TAG, "设置的start输入参数：" + params);
    myRecognizer.start(params);
  }

  @ReactMethod
  public void startWakeUp(){
    // DEMO集成步骤2.1 拼接识别参数： 此处params可以打印出来，直接写到你的代码里去，最终的json一致即可。
    final Map<String, Object> params = new HashMap<String, Object>();
    params.put(SpeechConstant.WP_WORDS_FILE, "assets:///WakeUp.bin");
    Log.i(TAG, "设置的start输入参数：" + params);
    myWakeup.start(params);
  }

  @ReactMethod
  public void stop(){
    myRecognizer.stop();
  }

  @ReactMethod
  public void stopWakeUp(){
    myWakeup.stop();
  }

  @ReactMethod
  public void cancel(){
    myRecognizer.cancel();
  }

  @ReactMethod
  public void destroy(){
    myRecognizer.release();
  }

  @ReactMethod
  public void destroyWakeUp(){
    myWakeup.release();
  }


  @Override
  public String getName() {
    return "RNBaiduAsr";
  }
}